package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Config.MyLog;
import com.realtyManage.springboot.Dao.CommentRepository;
import com.realtyManage.springboot.Dao.HouseDetailRepository;
import com.realtyManage.springboot.Model.Do.Comment;
import com.realtyManage.springboot.Model.Do.HouseDetail;
import com.realtyManage.springboot.Model.Vo.CommentEntity;
import com.realtyManage.springboot.Model.Vo.HouseEntity;
import com.realtyManage.springboot.Service.ICommentService;
import com.realtyManage.springboot.Service.IHouseDetailService;
import com.realtyManage.springboot.Service.ISpecialOfferHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ICommentService
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/9/24
 **/
@Service
@Transactional
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentRepository commentRepository;


    /*
    * @Author Danny
    * @Description //TODO 
    * @Date 2019/3/29
    * @Param [houseEntity]
    * @Return java.util.List<HouseDetail>
    **/
    @MyLog(value = "查询评论")
    @Override
    public Page<Comment> findPagedByWhereClause(CommentEntity commentEntity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer pageNumber = commentEntity.getPageNumber()-1;
        Integer pageSize = commentEntity.getPageSize();
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(pageNumber,pageSize,sort);

        //动态查询条件
        Specification<Comment> specification = (Specification<Comment>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (commentEntity.getCommentTime()!=null) {
                //根据幢号查询
                Predicate p0 = criteriaBuilder.like(root.get("commentTime"),'%'+commentEntity.getCommentTime()+'%');
                list.add(p0);
            }
            if (commentEntity.getHouseId()!=null){
                Predicate p1 = criteriaBuilder.equal(root.get("houseId"),commentEntity.getHouseId());
                list.add(p1);
            }
                Predicate p2 = criteriaBuilder.equal(root.get("type"),"1");
                list.add(p2);
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        Page<Comment> lst = commentRepository.findAll(specification, pageable);
        return lst;
    }


    @Override
    public int saveCommentInfo(Comment comment) {
        commentRepository.saveAndFlush(comment);
            return 1;
    }

    @Override
    public int deleteComment(String ids){
        List idList = new ArrayList();
        String id[] = ids.split(",");
        for (String z : id){
            idList.add(z);
        }
        return  commentRepository.deleteComment(idList);
    }

    @Override
    public Page<Comment> findUnaudited(CommentEntity commentEntity) {
        Integer pageNumber = commentEntity.getPageNumber()-1;
        Integer pageSize = commentEntity.getPageSize();
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(pageNumber,pageSize,sort);

        //动态查询条件
        Specification<Comment> specification = (Specification<Comment>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            Predicate p0 = criteriaBuilder.equal(root.get("type"),"0");
            list.add(p0);
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        Page<Comment> lst = commentRepository.findAll(specification, pageable);
        return lst;
    }

    @Override
    public int approvalComment(String id) {
        return commentRepository.approvalComment(id);
    }

    @Override
    public Comment findCommentById(Integer cid) {
        return commentRepository.getComment(cid);
    }

}
